InformIT - Your Online Guide to Tech Reference

Log OutShopping CartView Cart

HomeArticlesBook StoreOnline BooksAuthorsMyInformIT
Home > Articles > Web Development > More Topics... > .NET >

Save

 

Discuss

   

E-mail

Working With ADO.NET Transactions
OCT 25, 2002 By Jason Lefebvre. Article is provided courtesy of Sams.

Contents
1Hour 18: Working with Transactions
2Transactions and ADO.NET
3Transactions with Stored Procedures
4Summary
5Q&A
6Workshop
See Also

This sample chapter is from Sams Teach Yourself ADO.NET in 24 Hours.

Also By This Author
Using the ADO.NET's Built-In ASP.NET List Controls
Binding Data to List Controls with ADO.NET
Using ADO.NET's DataReader and DataAdapter
more…
Related Articles
Connected Operations in ADO.NET
Binding Data to List Controls with ADO.NET
Disconnected Operations Part I: DataSet and DataAdapter
more…
Related Books
Visual Basic® .NET Developer's Guide to ASP .NET, XML and ADO.NET
Database Programming with Visual Basic .NET and ADO.NET: Tips, Tutorials, and Code
more…

 

Advertise With Us

Transactions with Stored Procedures

Database transactions are by no means limited to ADO.NET. In fact, transactions are powered by the data source. That is to say, when you initiate a transaction inside ADO.NET, the data source (in this case Microsoft SQL Server) actually performs all the work.

To illustrate this point, the example in Listing 18.4 demonstrates how to perform a database transaction inside a stored procedure. It's very similar to an example used in a previous hour that adds an employee to the Employees table of the Northwind SQL Server database. This procedure also assigns a territory to the newly added employee as well, all rolled into a transaction.

To start a transaction inside a stored procedure in Microsoft SQL Server, you use the BEGIN TRAN keywords. To roll back a transaction, you use ROLLBACK TRAN and as you might have guessed, to commit a transaction, you use COMMIT TRAN.

Listing 18.4 Rolling a Transaction Back to a Saved Point

CREATE PROCEDURE Employee_Add
(
  @LastName nvarchar(20),
  @FirstName nvarchar(10),
  @Title nvarchar(30),
  @BirthDate datetime,
  @HireDate datetime,
  @Address nvarchar(60),
  @City nvarchar(15),
  @PostalCode nvarchar(10),
  @TerritoryID nvarchar(20)
)

AS

  DECLARE @iCommunityProductAuditID int

  BEGIN TRAN

  -- add the main record
  INSERT INTO Employees
  (
    LastName,
    FirstName,
    Title,
    BirthDate,
    HireDate,
    Address,
    City,
    PostalCode
  )
  VALUES
  (
    @LastName,
    @FirstName,
    @Title,
    @BirthDate,
    @HireDate,
    @Address,
    @City,
    @PostalCode
  )

  IF @@ERROR <> 0 
  BEGIN
   ROLLBACK TRAN
   RETURN @@ERROR
  END

    -- get EmployeeID
  declare @EmployeeID int
  SET @EmployeeID = @@IDENTITY

  -- add employee to a territory
    INSERT INTO EmployeeTerritories
    (
      EmployeeID,
    TerritoryID
    )
  VALUES
  (
    @EmployeeID,
    @TerritoryID
  )

  IF @@ERROR <> 0 
  BEGIN
   ROLLBACK TRAN
   RETURN @@ERROR
  END

  COMMIT TRAN

  RETURN @@ERROR
GO

The Employee_Add stored procedure in Listing 18.4 accepts a relatively large list of parameters in lines 3–11. As you'll recall from Hour 15, "Working with Stored Procedures," these are the same as function arguments. Line 18 begins a transaction within the stored procedure. Lines 21–42 add a record into the Employees table using the values supplied in the parameters.

If any errors were encountered while performing the INSERT query, the transaction is rolled back using the ROLLBACK TRAN SQL statement in lines 44–48. In line 52, the automatically incremented identity number created for the newly added employee is assigned to the @EmployeeID variable. The @EmployeeID is then used in lines 55–64 to add a territory for that new employee to the EmployeeTerritories table. Again, lines 66–70 ensure that if any errors were encountered, the entire transaction is rolled back; this means that not only will the EmployeeTerritories entry be removed, but also the entry made for the new employee in the Employees table. On line 72, the transaction is commited.


   
Next Section
4. Summary
Rate This Article
 
How would you rate this article?    

Most Active Comments
 
CommentAuthorRepliesPosted

 Spanning a TRX in varios method calls (may be connections)

peix

0OCT 29, 2002 07:07:04 AM
See All Comments 
 
Make a New Comment

Subject:

Username:


    Change Username

Message: